Home:ALL Converter>Can't import Python module even though it is in the PYTHONPATH

Can't import Python module even though it is in the PYTHONPATH

Ask Time:2022-10-09T22:39:15         Author:Julien

Json Formatter

I'm trying to run Pixar's USDZ converter tool but i can't seem to run the Python script, it complains with the error Error: failed to import pxr module. Please add path to USD Python bindings to your PYTHONPATH.

I am doing this on Ubuntu instead of the native MacOS, but i have compiled from source the USD library, located in /usr/local/USD/.

Runnig the script outputs the above error:

$ ./usdzconvert -h
  Error: failed to import pxr module. Please add path to USD Python bindings to your PYTHONPATH

even though I have added the path to the PYTHONPATH:

$ echo $PYTHONPATH
:/usr/local/USD/lib/python:/usr/local/USD/lib/python/pxr:/usr/local/USD/lib

furthermore Python seems to see that just fine:

$ python3 -c 'import sys; print(sys.path)'
['', '/home/julien', '/usr/local/USD/lib/python', '/usr/local/USD/lib/python/pxr', '/usr/local/USD/lib', '/usr/lib/python36.zip', '/usr/lib/python3.6', '/usr/lib/python3.6/lib-dynload', '/usr/local/lib/python3.6/dist-packages', '/usr/lib/python3/dist-packages']

I've also tried adding print(sys.path) to the begining of the script to confirm it was loading the same env variable and running it i get the same path as above.

The script portion that throws the error is as follows:

#!/usr/bin/env python3
import os.path
from os import chdir
import sys
import importlib
import tempfile
from shutil import rmtree
import zipfile

usdLibLoaded = True
kConvertErrorReturnValue = 2

if sys.version_info.major != 3:
    print('  \033[93mWarning: It is recommended to use Python 3. Current version is ' + str(sys.version_info.major) + '.\033[0m')

try:
    from pxr import *
    import usdUtils
except ImportError:
    print('  \033[91mError: failed to import pxr module. Please add path to USD Python bindings to your PYTHONPATH\033[0m')
    usdLibLoaded = False

I don't know anything about python so I am at loss to figure out this import issue, any ideas?

I have python 2 and 3 installed:

$ python --version
Python 2.7.17

python3 --version
Python 3.6.9

but considering the script starts off with a shebang instructing to use python3 i would think this would be fine, no?

Author:Julien,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/74005679/cant-import-python-module-even-though-it-is-in-the-pythonpath
Waket Zheng :

I don't known why, but sometimes, something like this worked for me:\nPYTHONPATH=. ./usdzconvert -h\n\nHow about this:\n#!/usr/bin/env python3\nimport os.path\nfrom os import chdir\nimport sys\nimport importlib\nimport tempfile\nfrom shutil import rmtree\nimport zipfile\n\nusdLibLoaded = True\nkConvertErrorReturnValue = 2\n\nif sys.version_info.major != 3:\n print(' \\033[93mWarning: It is recommended to use Python 3. Current version is ' + str(sys.version_info.major) + '.\\033[0m')\n\npxr_path = '/usr/local/USD/lib/python/pxr'\nsys.path.append(pxr_path)\nsys.path.append(os.path.dirpath(pxr_path))\n\ntry:\n from pxr import *\n import usdUtils\nexcept ImportError:\n print(' \\033[91mError: failed to import pxr module. Please check the path to USD\\033[0m')\n usdLibLoaded = False\n",
2022-10-09T15:31:09
yy